package com.igoal.dto;

import java.io.Serializable;
import java.util.Date;

import com.igoal.entity.CommentEntryDone;
import com.igoal.entity.User;
import com.igoal.entity.UserGoalDone;
import com.igoal.utils.CalendarUtils;
import com.igoal.utils.SynchronizeAttributeWithOther;
import com.igoal.utils.SynchronizeDirection;
import com.igoal.web.model.DummySettings;

public class CommentEntryDoneDTO extends BaseCommentDTO<UserGoalDone, CommentEntryDone> implements
		Serializable {
	private static final long serialVersionUID = -3396355219243530692L;
	
	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "user")
	public void initUserFromEntity(Object entity) {
		if (entity != null && entity instanceof User) {
			User user = (User) entity;

			this.ownerId = user.getId();
			this.ownerName = user.getUsername();
			if (user.getAvatar() != null) {
				this.avatarURL = user.getAvatar();
			} else {
				// TODO: remove this dummy setting after using default image
				// checker
				this.avatarURL = DummySettings.DEFAULT_AVATAR_URL;
			}
		}
	}
	
	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "parent")
	public void initParentFromEntity(Object entity) {
		if (entity != null && entity instanceof CommentEntryDone) {
			CommentEntryDone parent = (CommentEntryDone) entity;
			this.parentId = parent.getId();
		}
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "entry")
	public void initEntryFromEntity(Object entity) {
		if (entity != null && entity instanceof UserGoalDone) {
			UserGoalDone entry = (UserGoalDone) entity;
			this.entryId = entry.getId();
		}
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "updateDate")
	public void initUpdatedSinceFromEntity(Object entity) {
		if (entity != null && entity instanceof Date) {
			Date date = (Date) entity;
			this.updatedSince = CalendarUtils
					.calculateMinutesDifferentBeforeNow(date);
		}
	}
}
